package com.jt.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.annotation.CacheFind;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;
    @Autowired(required = false)
    private Jedis jedis;


    @Override
    public String  findItemCatName(Long itemCatId) {
        return itemCatMapper.selectById(itemCatId).getName();
    }

    @Override
    @CacheFind(preKey = "ITEM_CAT_PARENTID")
    public List<EasyUITree> findItemCatList(Long parentId) {
        List<EasyUITree> treeList=new ArrayList<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_id", parentId);
        List<ItemCat> catList=itemCatMapper.selectList(queryWrapper);
        for (ItemCat itemCat:catList){
            Long id=itemCat.getId();
            String text=itemCat.getName();
            String state=itemCat.getIsParent()?"closed":"open";
            EasyUITree easyUITree=new EasyUITree(id,text,state);
            treeList.add(easyUITree);
        }
        return treeList;
    }

//    @Override
//    public List<EasyUITree> findItemCatCache(Long parentId) {
//        Long startTime=System.currentTimeMillis();
//        List<EasyUITree> treeList=new ArrayList<>();
//        String key="ITEMCAT::"+parentId;
//        if (jedis.exists(key)){
//            String json=jedis.get(key);
//            treeList=ObjectMapperUtil.toObj(json,treeList.getClass());
//            Long endTime=System.currentTimeMillis();
//            System.out.println("查询redis缓存时间:"+(endTime-startTime)+"毫秒");
//        }else {
//            treeList=findItemCatList(parentId);
//            String json = ObjectMapperUtil.toJSON(treeList);
//            jedis.set(key,json);
//        }
//        return treeList;
//    }
}
